package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.Entity.SignUp;
import org.wmq.travelagency02.model.VO.SignUpQueryByTourGroupIdVO;
import org.wmq.travelagency02.model.VO.SignUpQueryByTravellerIdVO;
import org.wmq.travelagency02.model.VO.SignUpQueryVO;
import org.wmq.travelagency02.service.SignUpService;

import java.util.List;

@RestController
@RequestMapping("/sign-up")
@RequiredArgsConstructor
public class SignUpController {
    private final SignUpService signUpService;

    @GetMapping("/{travellerId}/{tourGroupId}")
    public Result<SignUpQueryVO> getSignUpInfoByTravellerIdAndTourGroupId(@PathVariable Long travellerId, @PathVariable Long tourGroupId){
        SignUpQueryVO result = this.signUpService.getSignUpInfoByTravellerIdAndTourGroupId(travellerId, tourGroupId);
        if (result != null){
            return Result.ok(result);
        }else {
            return Result.error("没有找到该用户报名信息");
        }
    }

    @GetMapping("/getByTravellerId/{travellerId}")
    public Result<SignUpQueryByTravellerIdVO> getSignUpInfoByTravellerId(@PathVariable Long travellerId){
        SignUpQueryByTravellerIdVO result = this.signUpService.getSignUpInfoByTravellerId(travellerId);
       if (result == null) {
           return Result.error("没有找到该用户报名信息");
       }
       return Result.ok(result);
    }

    @GetMapping("/getByTourGroupId/{tourGroupId}")
    public Result<SignUpQueryByTourGroupIdVO> getSignUpInfoByTourGroupId(@PathVariable Long tourGroupId){
        SignUpQueryByTourGroupIdVO result = this.signUpService.getSignUpInfoByTourGroupId(tourGroupId);
        if (result == null){
            return Result.error("没有找到该团报名信息");
        }
        return Result.ok(result);
    }

    @PostMapping
    public Result<Void> signUp(@RequestBody SignUp signUp){
        if (this.signUpService.save(signUp)){
            return Result.ok();
        }else {
            return Result.error("报名失败");
        }
    }

    @PutMapping
    public Result<Void> updateSignUpStatus(@RequestBody SignUp signUp){
        if (this.signUpService.updateSignUpById(signUp)){
            return Result.ok();
        }else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{travellerId}/{tourGroupId}")
    public Result<Void> deleteSignUpInfoByTravellerIdAndTourGroupId(@PathVariable Long travellerId, @PathVariable Long tourGroupId){
        boolean success = this.signUpService.lambdaUpdate().eq(SignUp::getTravellerId, travellerId)
                .eq(SignUp::getTourGroupId, tourGroupId).remove();
        if (!success){
            return Result.error("删除失败");
        }
        return Result.ok();
    }

}
